/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.uff.pgci.sgca.gateway.server;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Essa eh a classe que inicia o gateway SGCA
 * 
 * @author thiagodiogo
 */
public class PrincipalGateway {

    private static final Logger logger = Logger.getLogger(PrincipalGateway.class.getName());

    public static void main(String[] args) {
        logger.info("Iniciando o Gateway SGCA...");

        logger.info("1) Iniciando o contexto do Spring");
        ApplicationContext context = new ClassPathXmlApplicationContext("gateway-sgca-config.xml");

        final ServidorUDP servidorUDP = (ServidorUDP) context.getBean(br.uff.pgci.sgca.gateway.server.ServidorUDP.class);

        try {
//            logger.info("2) Iniciando o servidor UDP do Gateway SGCA...");
            servidorUDP.iniciarServidorUDP();
//            logger.info("...servidor UDP do Gateway SGCA iniciado com sucesso");
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Erro ao iniciar o servidor UDP do Gateway SGCA, abortando.", ex);
            System.exit(1);
        }

//        logger.info("... Gateway SGCA iniciado com sucesso");

        // Finalizacao do servidor UDP (intercept shutdown signal from VM and shut-down gracefuly)
        // So desconecta o servidor ao matar a VM
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                servidorUDP.finalizarServidorUDP();
            }
        }, "shutdownHook"));

    }
}
